package org.mzx.shortLink.adminn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

import org.mzx.shortLink.adminn.remoteApi.workkApi.feignLinkApi;
import org.mzx.shortLink.adminn.remoteApi.dto.Request.TLinkCountDtoReq;
import org.mzx.shortLink.adminn.remoteApi.dto.Request.TLinkCreateDtoReq;
import org.mzx.shortLink.adminn.remoteApi.dto.Request.TLinkUpdateDtoReq;
import org.mzx.shortLink.adminn.remoteApi.dto.Response.TLinkCreateDtoResp;
import org.mzx.shortLink.adminn.remoteApi.dto.Response.TLinkPageDtoResp;
import org.mzx.shortLink.starter.convention.Result.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
public class TLinkController {
    /**
     * 直接走http调用，调用work的api
     */
    private final feignLinkApi service;

    /**
     * 创建短链接
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/create")
    public Result<TLinkCreateDtoResp> create(@RequestBody TLinkCreateDtoReq requestParam) {

        return service.create(requestParam);
    }

    /**
     * 分页查询短链接
     * @param gid
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/page")
    public Result<IPage<TLinkPageDtoResp>> getByPage(@RequestParam("gid") String gid, @RequestParam("current") Long current, @RequestParam("size") Long size, @RequestParam(value = "orderTag",required = false) String orderTag) {

        return service.getByPage(gid,current,size,orderTag);
    }

    /**
     * 根据gid返回其组下面的短链接总数
     * @param requestParam
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/count")
    public Result<Map<String, Map<String, Object>>> count(@RequestBody List<TLinkCountDtoReq> requestParam) {
        return service.count(requestParam);
    }

    /**
     * 修改短链接
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/update")
    public Result update(@RequestBody TLinkUpdateDtoReq requestParam) {
        return service.update(requestParam);
    }

    /**
     * 根据完整url获取网站的标题
     * @param url
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/title")
    Result<String> getTitleByUrl(@RequestParam("url") String url) {
        return service.getTitleByUrl(url);
    }

    /**
     * admin调用短链接中控台
     * @param shortUrl
     */
    @GetMapping("/{short-uri}")
    public void reDirect(@PathVariable(value = "short-uri")String shortUrl) {
        service.reDirect(shortUrl);
    }
}
